<template>
    <div class="wrap">
      <el-result icon="success" title="授权成功" subTitle="正在进入系统">
        <template slot="extra">
          <el-button :loading="loading" type="primary" size="medium" @click="getToken(code)">
            <span v-if="!loading">立即跳转</span>
            <span v-else>跳 转 中...</span>
          </el-button>
        </template>
      </el-result>
    </div>
</template>

<script>
    export default {
        name: "loading",
        components: {},
        data() {
            return {
              code: '',
              loading: false,
              redirect: undefined
            }
        },
      watch: {
        $route: {
          handler: function(route) {
            this.redirect = route.query && route.query.redirect;
          },
          immediate: true
        }
      },
        mounted() {
          let code = this.getUrlKey('code')
          let url = this.getUrl(window.location).then(res=>{
            this.loading = true
            this.$store.dispatch("AUTHLogin", { code }).then(() => {
              // this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
              window.parent.refreshPage('父页面执行跳转路由')
            }).catch(() => {
              this.loading = false
            })
          })
          this.code = code
        },
        methods: {
          getUrl(obj){
            return new Promise((resolve, reject) => {
              const { origin, pathname, hash, search } = obj
              let url = origin + pathname + hash + search
              window.history.pushState({},0, url)
              resolve(url)
            })
          },
          getUrlKey(name) {
            return decodeURIComponent(
              (new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(window.location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
          }
        }
    }
</script>

<style scoped>
 .wrap{
   height: 100%;
   display: flex;
   align-items: center;
   justify-content: center;
 }
</style>
